﻿$(document).ready(function () {
    $("#province").bind("change", bindCity);
    $("#city").bind("change", bindArea);
    $("#ubtnSubmit").bind("click", submitUserAddress);
    $("a[name='addressSelect']").bind("click", addressSelect)
    $("a[name='updateAddress']").bind("click", updateAddress);

    $("#commonAddress").click("click", ReSetUserAddress);
});



function ReSetUserAddress() {

    commonaddressid = 0;
    $("#useraddresstable input").each(function () { $(this).val(""); });

    $("#province>option").eq(0).attr("selected", "selected");
    bindCity();
    $("#isdefault").attr("checked", false);
}

function addressSelect() {
    var tr = $(this).parents("tr");
    if (tr) {
        var ret = {};
        eval("ret=" + tr.attr("tag") + ";");
        if (ret._useraddressid) {
            var success = function (city) {

                commonaddressid = ret._useraddressid;
                addressId = commonaddressid;
                var html = Array();
                html.push("<div class=\"divrow\"><dl><dt>收货人姓名：</dt><dd>" + ret._receivername + "</dd></dl></div>");
                if (city) {
                    eval("city=" + city);
                    html.push("<div class=\"divrow\"><dl><dt>省 份：</dt><dd>" + city.province.Name + city.city.Name + city.area.Name + "</dd></dl></div>");
                }
                html.push("<div class=\"divrow\"><dl><dt>地 址：</dt><dd>" + ret._receiveraddress + "</dd></dl></div>");
                html.push("<div class=\"divrow\"><dl><dt>手机号码：</dt><dd>" + ret._receivermobile + "</dd></dl></div>");
                html.push("<div class=\"divrow\"><dl><dt>固定电话：</dt><dd>" + ret._receiverphone + "</dd></dl></div>");
                html.push("<div class=\"divrow\"><dl><dt>邮政编码：</dt><dd>" + ret._receiverzipcode + "</dd></dl></div>");
                $("#divReciveInfo>.orderinfo").html(html.join(""));
                $("#divReciveInfo").show();
                $("#divaddressU").hide();

            };
            var posttype = "GET";
            var posturl = "/as/ajaxcommon.aspx?r=" + Math.random();
            AjaxPostCommon("GetAddressByAreaID", "areaid=" + ret._areaid, success, posttype, "", posturl);

        }
    }

}

function updateAddress() {
    var tr = $(this).parents("tr");
    if (tr) {
        var ret = {};
        eval("ret=" + tr.attr("tag") + ";");
        if (ret._useraddressid) {
            var success = function (city) {
                commonaddressid = ret._useraddressid;
                addressId = commonaddressid;
                $("#txtReciveName").val(ret._receivername);
                $("#txtReciveAddress").val(ret._receiveraddress);
                $("#txtReciveZip").val(ret._receiverzipcode);
                $("#txtReciveMobile").val(ret._receivermobile);
                $("#txtRecivephone").val(ret._receiverphone);
                $("#isdefault").attr("checked", ret._isdefault);
                if (city) {
                    eval("city=" + city);
                    $("#province>option:selected").removeAttr("selected");
                    $("#province>option[value='" + city.province.ID + "']").attr("selected", "selected");
                    bindCity(city.city.ID, city.area.ID);
                }

                //$("#TB_closeWindowButton").click();
            };
            var posttype = "GET";
            var posturl = "/as/ajaxcommon.aspx?r=" + Math.random();
            AjaxPostCommon("GetAddressByAreaID", "areaid=" + ret._areaid, success, posttype, "", posturl);
        }
    }
}

function submitUserAddress() {
    var recivalName = $.trim($("#txtReciveName").val());
    var area = $.trim($("#area>option:selected").val());
    var ReciveAddress = $.trim($("#txtReciveAddress").val());
    var ReciveZip = $.trim($("#txtReciveZip").val());
    var ReciveMobile = $.trim($("#txtReciveMobile").val());
    var Recivephone = $.trim($("#txtRecivephone").val());
    var isdefault = $("#isdefault").attr("checked");

    var data = Array();
    var Action = "AddUserAddress";
    if (commonaddressid > 0) {
        Action = "UpdateUserAddress";
        data.push("addressid=" + commonaddressid);
    }
    data.push("recivalName=" + recivalName);
    data.push("area=" + area);
    data.push("reciveAddress=" + ReciveAddress);
    data.push("ReciveZip=" + ReciveZip);
    data.push("ReciveMobile=" + ReciveMobile);
    data.push("Recivephone=" + Recivephone);
    data.push("isdefault=" + isdefault);

    var posttype = "GET";
    var datatype = "JSON";

    var success = function (ret) {
        eval("ret=" + ret);
        if (ret.Result) {
            alert("操作成功");
            //$("#TB_closeWindowButton").click();
            window.location.href = window.location.href;

        }
        else {
            alert(ret.Message);
        }
    };
    var posturl = "/home/as/AjaxHomeCommon.aspx?r=" + Math.random();

    AjaxPostCommon(Action, data.join("&"), success, posttype, datatype, posturl);

}

function bindCity(cityid, areaid) {
    var Action = "GetCitiesByProvinceID";
    var data = "ProvinceID=" + $("#province").val();
    $("#city>option").remove();
    $("#area>option").remove();
    var success = function (ret) {
        if (ret) {
            eval("ret=" + ret);
            var html = Array();
            for (var i = 0; i < ret.length; i++) {
                html.push("<option value='" + ret[i]._id + "' " + (cityid && cityid == ret[i]._id ? "selected='selected'" : "") + ">" + ret[i]._name + "</option>");
            }

            $("#city").html(html.join(""));

            bindArea(areaid);
        }
    }
    var posttype = "GET";
    var datatype = "JSON";
    AjaxPostCommon(Action, data, success, posttype, datatype);
}

function bindArea(areaid) {
    var Action = "GetAreasByCityID";
    var data = "CityID=" + $("#city").val();
    $("#area>option").remove();
    var success = function (ret) {
        if (ret) {
            eval("ret=" + ret);
            var html = Array();
            for (var i = 0; i < ret.length; i++) {
                html.push("<option value='" + ret[i]._id + "'  " + (areaid && areaid == ret[i]._id ? "selected='selected'" : "") + ">" + ret[i]._name + "</option>");
            }

            $("#area").html(html.join(""));
        }
    }
    var posttype = "GET";
    var datatype = "JSON";
    AjaxPostCommon(Action, data, success, posttype, datatype);
}

function AjaxPostCommon(Action, data, success, posttype, datatype, posturl) {
    var url = "/as/AjaxCommon.aspx?r=" + Math.random();

    var ajaxurl = (posturl ? posturl : url) + "&Action=" + Action;

    var param = {
        type: posttype,
        url: ajaxurl,
        data: data,

        success: success
    };
    if (datatype) {
        param.datatype = datatype;
    }
    $.ajax(param);
}
